import { System } from "laya-system";
import Player from "../../source/main/Player";

import GameMap from "../../source/main/Map";
import MiniMap from "../../source/main/MiniMap";
import { Tracking } from "game-algorithm";
import { mainStore } from "source/store/main";
import { BlackBoard, hook, loadPrefabAsBlackBoard } from "laya-system";

export default class index extends Laya.Script {
    /** @prop { name:WallContainer, tips:"", type:Node, default:null} */
    /** @prop { name:ChestContainer, tips:"", type:Node, default:null} */

    WallContainer!: Laya.Sprite;
    ChestContainer!: Laya.Sprite;

    //! 注意这里使用了 随机种子
    seed?: string = "KonghaYao";
    map!: GameMap;
    player!: Player;
    static Prefab: BlackBoard<{
        Bullet: Laya.Prefab;
        Wall: Laya.Prefab;
        WallFake: Laya.Prefab;
        Player: Laya.Prefab;
        Chest: Laya.Prefab;
    }>;
    constructor() {
        super();
        Laya.stage.bgColor = "#fff";
    }
    stage!: Laya.Dialog;

    onStart(): void {
        [
            async () => {
                index.Prefab = await loadPrefabAsBlackBoard({
                    Bullet: {},
                    Wall: {},
                    WallFake: {},
                    Player: {},
                    Chest: {},
                });
            },
            async () => {
                this.map = new GameMap({
                    width: 50,
                    height: 50,

                    minRoomSize: 100,
                    smoothTime: 4,
                    density: 0.4,
                    seed: this.seed,
                }).buildMap();

                // 布置墙壁
                const Walls = this.map.createWalls(
                    index.Prefab.get("Wall")!,
                    index.Prefab.get("WallFake")!
                );
                this.WallContainer.addChildren(...Walls);

                // 布置箱子
                const chests = this.map.createChest(index.Prefab.get("Chest")!);
                this.ChestContainer.addChildren(...chests);
            },

            async () => {
                // player 声明开始
                this.player = new Player(index.Prefab.get("Player")!);

                this.player.InitKeyBind();
                console.log("a");
                const [x, y] = this.map.pickPlace();

                this.player.placeToStage(
                    [x * this.map.scale, y * this.map.scale],
                    [this.map.maxWidth, this.map.maxHeight]
                );
                this.owner.addChild(this.player.player);
                mainStore.set("Player", this.player);
                (window as any).player = this.player;
            },

            async () => {
                // 跟随小球
                const target = this.player.player;
                const bullet = index.Prefab.get("Bullet").create();
                const tra = new Tracking({
                    target,
                    tracker: bullet,
                    getPosition(i) {
                        return i;
                    },
                    ctrConfig: {},
                    space: 100,
                });
                bullet.pos(target.x, target.y);
                this.owner.addChild(bullet);
                setTimeout(() => {
                    const RigidBody = bullet.getComponent(
                        Laya.RigidBody
                    ) as Laya.RigidBody;
                    bullet.frameLoop(20, null, () => {
                        const { x, y } = tra.update();

                        // console.log(correction);
                        RigidBody.setVelocity({
                            x: x * 1,
                            y: y * 1,
                        });
                    });
                }, 1000);
            },
            () => {
                // 小地图布置
                const callback = new Laya.Handler(
                    null,
                    (MiniMapDOM: Laya.Dialog) => {
                        console.log(MiniMapDOM);

                        hook(MiniMapDOM, "onOpened", {
                            after() {
                                console.log("开启了");
                                MiniMapDOM.x = 0;
                                MiniMapDOM.y = 0;

                                // MiniMapDOM.pos(0, 0, true);
                            },
                        });
                        const miniMap = new MiniMap(
                            MiniMapDOM,
                            this.map.map,
                            9
                        );
                        console.log("开始渲染小地图");
                        miniMap.render(this.player.player, this.map.scale);
                    },
                    null,
                    true
                );
                Laya.Dialog.open("MiniMap.scene", false, ["1", "2"], callback);
            },
            () => {
                this.player.useGun(index.Prefab.get("Bullet")!);
            },
            () => {
                System.Camera.setStage(this.owner as Laya.Sprite);

                System.Camera.tracking(this.player.player);
                System.Keybind.init();
                System.Mouse.init();
                System.FocusRender.trackingToCamera(this.map.Walls);
                System.Mouse.on("pointerdown", (e) => {
                    console.log(e.type);
                    console.log(e.getWorldPos!());
                    console.log(e.getCameraCenterPos!());
                });
            },

            () => {},
        ].reduce((promise, cur) => {
            return promise.then(async () => cur.bind(this)());
        }, Promise.resolve());
    }
    onDisable(): void {
        System.Camera.stopTrack();
        System.Camera.setStage(Laya.stage);
        console.log("程序被删除");
    }
    onDestroy(): void {
        mainStore.delete("Player");
    }
}
